<?php include './application/views/common/header.php';?>
	document.title= '权限管理';
	Ext.navActive = 'system';
	Ext.menuUrl = list.baseUrl+'permission';
	
	<?php include './application/views/common/system.menu.php';?>

	var attr = {};

	list.grid = new ZM.GridPanel({
		url: list.baseUrl + 'permission/getPermissionList',
		history:'permission_id',
		bbar:null,
		autoExpandColumn : 'action',
		tbar:[{
			text:'添加权限',
			cls:'btn-show',
			hidden:!Ext.permission['permission/createPermission'.toLowerCase()],
			iconCls:'icon-create',
			handler:function() {
				var frm = new ZM.FormPanel({
					url:list.baseUrl + 'permission/createPermission',
					keySubmit:false,
					success:function(res) {
						ZM.bo.msg('success',res.detail);
						list.grid.store.load();
						win.close();
					},
					items:[{
						xtype:'textfield',
						name:'permission_name',
						fieldLabel:'名称',
						allowBlank:false
					},{
						xtype:'textfield',
						name:'permission_category',
						fieldLabel:'类别',
						allowBlank:false
					}]
				});
				var win = new ZM.PopUp({
					title:'新建权限',
					type:'s',
					items:frm
				});
				win.show();
			}
		}],
		group:{'field':'permission_category','groupView':true},
		columns:[
			{header:'ID',dataIndex:'permission_id',hidden:true},
			{header:'权限',dataIndex:'permission_name',width:120},
			{header:'权限分类',dataIndex:'permission_category',width:150},
			{header:'Action数量',dataIndex:'action_count',width:80},
			{header:'操作',dataIndex:'',id:'action',renderer:function(val,m,r) {
				str = '';
				str += Ext.rd('编辑名称','fireEditAction',r,'permission/editPermission');
				str += Ext.rd('编辑权限','fireEditPermission',r,'permission/getActionList');
				return str;
			}}
		]
	});

	list.grid.fireEditAction = function(r) {
		var r = r.json;
		var frm = new ZM.FormPanel({
			url:list.baseUrl + 'permission/editPermission',
			params:{permission_id:r.permission_id},
			keySubmit:true,
			success:function(res) {
				ZM.bo.msg('success',res.detail);
				list.grid.store.load();
				win.close();
			},
			items:[{
				xtype:'textfield',
				name:'permission_name',
				fieldLabel:'名称',
				allowBlank:false,
				value:r.permission_name
			},{
				xtype:'textfield',
				name:'permission_category',
				fieldLabel:'类别',
				allowBlank:false,
				value:r.permission_category
			}]
		});
		var win = new ZM.PopUp({
			title:'编辑权限['+r.permission_name+']',
			type:'s',
			items:frm
		});
		win.show();
	}

	list.grid.fireEditPermission = function(r) {
		var r = r.json
		var actionGrid = new ZM.GridPanel({
			url: list.baseUrl + 'permission/getActionList',
			history:'id',
			noRowNumber : true,
			region : 'center',
			autoExpandColumn : 'action',
			group:{'field':'controller','groupView':true},
			bbar:null,
			sm:sm,
			tbar:[{
				text:'保存',
				cls:'btn-show',
				hidden:!Ext.permission['permission/editAction'.toLowerCase()],
				handler:function() {
					var datas = actionGrid.getSelectionModel().selections;
					var params = {};
					if(datas.items.length == 0) {
						Ext.Msg.alert('提示','未选中action!');
						return;
					}
					Ext.each(datas.items,function(val,i) {
						params['action['+i+']'] = val.json.controller+'.'+val.json.action;
					});
					actionAction('permission/editAction',params);
				}
			}],
			columns:[
				sm,
				{header: "controller",hidden:true, dataIndex: 'controller'},
				{header: "action",dataIndex: 'action',id:'action'},
				{header: "状态",dataIndex: 'status',renderer:ZM.status}
			],
			listeners : {
				'afterrender' : function(grid){
					grid.store.on('load', function(store, records, options){
						var rows = [];
						Ext.each(records,function(r,i){
							if(r.get('status')){
								rows.push(store.indexOf(r));//row index
							};
						});
						grid.getSelectionModel().selectRows(rows,true);
					}); 
				}
			}
		});
		actionGrid.fireAddAction = function(nr) {
			var nr = nr.json;
			var params = {};
			params['action[0]'] = nr.controller+'.'+nr.action;
			actionAction('permission/addAction',params);
		}
		actionGrid.fireRemoveAction = function(nr) {
			var nr = nr.json;
			var params = {};
			params['action[0]'] = nr.controller+'.'+nr.action;
			actionAction('permission/removeAction',params);
		}
		function actionAction(url,params) {
			ZM.ajax({
				url:list.baseUrl + url,
				params:Ext.apply(params,{permission_id:r.permission_id}),
				success:function(res) {
					ZM.bo.msg('success',res.detail);
					actionGrid.store.reload();
					list.grid.store.reload();
				},
				failure:function(res) {
					ZM.bo.msg('failed',res.detail);
				}
			});
		}
		var win = new ZM.PopUp({
			title:'编辑['+r.permission_name+']Action',
			items:actionGrid,
			type:'l',
			width:550
		});
		win.show();
		actionGrid.store.baseParams = {permission_id:r.permission_id};
		actionGrid.store.load();
	}
	<?php include './application/views/common/layout.php';?>
	list.grid.store.load();
<?php include './application/views/common/footer.php';?>